import Vue from 'vue'
import Router from 'vue-router'
import Cookies from 'js-cookie';
const Index = () => import('@views/layout/index');
import HOME_ROUTERS from '../views/home/route'
import PROPAGANDA_ROUTERS from '../views/propaganda/route'
import ORGANIZATION_ROUTERS from '../views/organization/route'
import LEARN_ROUTERS from '../views/learn/route'
import MINE_ROUTERS from '../views/mine/route'
import SIGN_ROUTERS from '../views/sign/route'


Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: '/',
            name: 'home',
            component: Index,
            redirect: 'home',
            children: [
                ...HOME_ROUTERS,
                ...PROPAGANDA_ROUTERS,
                ...ORGANIZATION_ROUTERS,
                ...LEARN_ROUTERS,
                ...MINE_ROUTERS,
                ...SIGN_ROUTERS

                // ...DEMO_ROUTERS
            ],
            meta: {
                index: 0, // 层级，用于判断过渡动画的效果
                requireAuth: false, // 添加该字段，表示进入这个路由是需要登录的
            }
        }
    ]
})

const defaultTitle = '智慧云支部'
router.beforeEach(function (to, from, next) {
    window.scrollTo(0,0);
    console.log(to.path);
    // if (to.meta.title) {  // || store.state.title
    //     Vue.refreshTitle(to.meta.title); //|| store.state.title
    // }
    if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
        console.log(Cookies.get('user_71ydj'))
        if (Cookies.get('user_71ydj')) { // 查询本地存储信息是否已经登陆
            next();
        } else {
            if(to.path!=='login'){
                next('/login');
            }
            // next({
            //     path: '/login', // 未登录则跳转至login页面
            //     query: { redirect: to.fullPath } // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面
            // });
        }
    } else {
        next();
        // document.title = to.meta.title ? to.meta.title : defaultTitle
    }

    // next();
});

export default router